package com.planegame;

import com.sonymobile.smartplane.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;

/**
 * @author Sunrise
 */
public class Player {
    //大招
    public static int BigKillFlag = 0;
    //主角的血量与血量位图
    //默认5血
    private int playerHp = 5;
    private Bitmap bmpPlayerHp;
    //大招默认3个
    private int BigKillNum = 3;
    private Bitmap bmpBigKill;
    //分数样式0-9图片
    private Bitmap bmpnum[];
    Bitmap numF[] = new Bitmap[11];        //缩放后分数图片
    private int ge, shi, bai;        //个十百位数字
    //游戏积分
    private int grade;
    //主角的坐标以及位图
    public int x, y;
    public Bitmap bmpPlayer;
    //主角移动速度
    private float speedx, speedy;
    //	private float speed = Math.abs(MainActivity.x);
    //主角移动标识（基础章节已讲解，你懂得）
    private boolean isUp, isDown, isLeft, isRight;
    //碰撞后处于无敌时间
    //计时器
    private int noCollisionCount = 0;
    //因为无敌时间
    private int noCollisionTime = 60;
    //是否碰撞的标识位
    private boolean isCollision;


    //主角的构造函数
    public Player(Bitmap bmpPlayer, Bitmap bmpPlayerHp, Bitmap bmpnum[], Bitmap bmpBigKill) {
        this.bmpPlayer = bmpPlayer;
        this.bmpPlayerHp = bmpPlayerHp;
        this.bmpnum = bmpnum;
        this.bmpBigKill = bmpBigKill;
        x = MySurfaceView.screenW / 2 - bmpPlayer.getWidth() / 2;
        y = MySurfaceView.screenH - bmpPlayer.getHeight();
    }

    //主角的绘图函数
    public void draw(Canvas canvas, Paint paint) {
        //绘制主角
        //当处于无敌时间时，让主角闪烁
        if (isCollision) {
            //每2次游戏循环，绘制一次主角
            if (noCollisionCount % 2 == 0) {
                canvas.drawBitmap(bmpPlayer, x, y, paint);
            }
        } else {
            canvas.drawBitmap(bmpPlayer, x, y, paint);
        }
        //绘制主角血量
        for (int i = 0; i < playerHp; i++) {
            canvas.drawBitmap(bmpPlayerHp, i * bmpPlayerHp.getWidth(), 0, paint);
        }
        //绘制大招数量
        for (int i = 0; i < BigKillNum; i++) {
            canvas.drawBitmap(bmpBigKill, i * (bmpBigKill.getWidth() + 2), MySurfaceView.screenH - bmpBigKill.getHeight() - 2, paint);
        }
        //绘制积分
        drawGrade();
        canvas.drawBitmap(bmpnum[ge], MySurfaceView.screenW - bmpnum[0].getWidth(), bmpnum[0].getHeight() / 2, paint);
        canvas.drawBitmap(bmpnum[shi], MySurfaceView.screenW - 2 * bmpnum[0].getWidth(), bmpnum[0].getHeight() / 2, paint);
        canvas.drawBitmap(bmpnum[bai], MySurfaceView.screenW - 3 * bmpnum[0].getWidth(), bmpnum[0].getHeight() / 2, paint);
//	   	canvas.drawBitmap(numF[bai], MySurfaceView.screenW*80/720, MySurfaceView.screenH*120/1200 , paint);
//	   	canvas.drawBitmap(numF[shi], MySurfaceView.screenW*80/720 + MySurfaceView.screenH*20/720, MySurfaceView.screenH*120/1200 , paint);
//	   	canvas.drawBitmap(numF[ge], MySurfaceView.screenW*80/720 + 2*MySurfaceView.screenH*20/720, MySurfaceView.screenH*120/1200, paint);
//	    canvas.drawBitmap(numF[10], 18*screenW/720, 120*screenH/1200, paint);
    }

    //绘制成绩部分
    private void drawGrade() {
        // TODO Auto-generated method stub
//		   	for(int i=0; i<11; i++)
//		   	{
//		   		numF[i] = scaleImg(bmpnum[i], (float) MySurfaceView.screenW/720, (float) MySurfaceView.screenH/1280);		//分数样式0-9图片缩放
//		   	}
        if (grade <= 0)        //防止负数
            ge = shi = bai = 0;
        else {
            ge = grade % 10;
            shi = (grade / 10) % 10;
            bai = grade / 100;
        }
    }

    public void onTouchEvent(MotionEvent event) {
        int pointX = (int) event.getX();
        int pointY = (int) event.getY();
        //判断屏幕X边界
        if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
            x = MySurfaceView.screenW - bmpPlayer.getWidth();
        } else if (x <= 0) {
            x = 0;
        }
        //判断屏幕Y边界
        if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
            y = MySurfaceView.screenH - bmpPlayer.getHeight();
        } else if (y <= 0) {
            y = 0;
        }
        x = pointX - bmpPlayer.getWidth() / 2;
        y = pointY - bmpPlayer.getHeight() * 3 / 4;
    }

    //主角的逻辑
    public void logic() {
        if (MainActivity.z < -5 && BigKillFlag == 0) {
            BigKillFlag = 2;
            BigKillNum--;
        }
        if (MainActivity.z > 0 && BigKillFlag == 1) {
            BigKillFlag = 0;
        }
        //处理主角移动
        speedy = 3 * Math.abs(MainActivity.x);
        speedx = 3 * Math.abs(MainActivity.y);
        if (speedx < 7)
            speedx = 7;
        else if (speedx > 20)
            speedx = 20;
        if (speedy < 7)
            speedy = 7;
        else if (speedy > 20)
            speedy = 20;
//		speed = 10;
        if (MainActivity.y > 0) {
            x -= speedx;
        }
        if (MainActivity.y < 0) {
            x += speedx;
        }
        if (MainActivity.x > 0) {
            y -= speedy;
        }
        if (MainActivity.x < 0) {
            y += speedy;
        }
//		if (isLeft) {
//			x -= speedx;
//		}
//		if (isRight) {
//			x += speedx;
//		}
//		if (isUp) {
//			y -= speedy;
//		}
//		if (isDown) {
//			y += speedy;
//		}
        //判断屏幕X边界
        if (x + bmpPlayer.getWidth() >= MySurfaceView.screenW) {
            x = MySurfaceView.screenW - bmpPlayer.getWidth();
        } else if (x <= 0) {
            x = 0;
        }
        //判断屏幕Y边界
        if (y + bmpPlayer.getHeight() >= MySurfaceView.screenH) {
            y = MySurfaceView.screenH - bmpPlayer.getHeight();
        } else if (y <= 0) {
            y = 0;
        }
        //处理无敌状态
        if (isCollision) {
            //计时器开始计时
            noCollisionCount++;
            if (noCollisionCount >= noCollisionTime) {
                //无敌时间过后，接触无敌状态及初始化计数器
                isCollision = false;
                noCollisionCount = 0;
            }
        }
    }

    //设置主角血量
    public void setPlayerHp(int hp) {
        this.playerHp = hp;
    }

    //获取主角血量
    public int getPlayerHp() {
        return playerHp;
    }

    //设置分数
    public void setGrade(int grade) {
        this.grade = grade;
    }

    //获取分数
    public int getGrade() {
        return grade;
    }

    //设置大招数量
    public void setBigKill(int BigKillNum) {
        this.BigKillNum = BigKillNum;
    }

    //获取大招数量
    public int getBigKill() {
        return BigKillNum;
    }

    //判断碰撞(主角与敌机)
    public boolean isCollsionWith(Enemy en) {
        //是否处于无敌时间
        if (isCollision == false) {
            int x2 = en.x;
            int y2 = en.y;
            int w2 = en.frameW;
            int h2 = en.frameH;
            if (x + bmpPlayer.getWidth() / 4 >= x2 + w2) {
                return false;
            } else if (x + bmpPlayer.getWidth() * 3 / 4 <= x2) {
                return false;
            } else if (y + bmpPlayer.getHeight() / 4 >= y2 + h2) {
                return false;
            } else if (y + bmpPlayer.getHeight() * 3 / 4 <= y2) {
                return false;
            }
            //碰撞即进入无敌状态
            isCollision = true;
            return true;
            //处于无敌状态，无视碰撞
        } else {
            return false;
        }
    }

    //判断碰撞(主角与敌机子弹)
    public boolean isCollsionWith(Bullet bullet) {
        //是否处于无敌时间
        if (isCollision == false) {
            int x2 = bullet.bulletX;
            int y2 = bullet.bulletY;
            int w2 = bullet.bmpBullet.getWidth();
            int h2 = bullet.bmpBullet.getHeight();
            if (x >= x2 + w2 / 2) {
                return false;
            } else if (x + bmpPlayer.getWidth() <= x2 + w2 / 2) {
                return false;
            } else if (y + bmpPlayer.getHeight() / 2 >= y2 + h2 / 2) {
                return false;
            } else if (y + bmpPlayer.getHeight() <= y2 + h2 / 2) {
                return false;
            }
            //碰撞即进入无敌状态
            isCollision = true;
            return true;
            //处于无敌状态，无视碰撞
        } else {
            return false;
        }
    }

    //缩放图片函数
    public Bitmap scaleImg(Bitmap bm, float scaW, float scaH) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaW, scaH);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }
}
